package ihm;

import metier.*;

import java.awt.EventQueue;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Ihm extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private GroupLayout gl_contentPane;
	private JTabbedPane tabbedPane;
	private JButton btnAjouter;
	private JButton btnModifier;
	private JButton btnSupprimer;
	private JButton btnQuitter;
	
	// Variables pour l'onglet Tâche
	private JPanel pnlTache;
	private GroupLayout gl_pnlTache;
	
	private ArrayList<Tache> lesTaches;
	private JLabel lblTaches; private JComboBox<Tache> cboTaches;
	private JLabel lblNumero; private JLabel lblRepNumero;
	private JLabel lblLibelle; private JLabel lblRepLibelle;
	private JLabel lblJourHomme; private JLabel lblRepJourHomme;
	private JLabel lblMatriculeCollab; private JLabel lblRepMatriculeCollab;
	private JLabel lblRefContrat; private JLabel lblRepRefContrat;
	private IhmTache monIhmTache;
	
	// Variables pour l'onglet Collaborateur
	private JComboBox<Collaborateur> cboCollabs;
	private ArrayList<Collaborateur> lesCollabs;
	
	//Variables pour l'onglet Contrat
	public JComboBox<Contrat> cboContrats;
	private ArrayList<Contrat> lesContrats;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				
				try {
					Ihm frame = new Ihm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Ihm() {
		
		monIhmTache = new IhmTache();
		/* 
		 * Déclaration de la collection lesTaches et ajout
		 */
		lesTaches = new ArrayList<Tache>();
		Tache uneTache1 = new Tache(1,"Cowcow",2,3,4);
		lesTaches.add(uneTache1);
		Tache uneTache2 = new Tache(2,"Coucou",4,3,2);
		lesTaches.add(uneTache2);
		Tache uneTache3 = new Tache(3,"Salut",5,6,7);
		lesTaches.add(uneTache3);
		/*
		 * Fin déclaration lesTaches
		 */
		
		lesCollabs = new ArrayList<Collaborateur>();
		Collaborateur unCollab1 = new Collaborateur(1, "Dupont", "Didier");
		Collaborateur unCollab2 = new Collaborateur(2, "Durand", "Claude");
		lesCollabs.add(unCollab1);
		lesCollabs.add(unCollab2);
		
		lesContrats = new ArrayList<Contrat>();
		Contrat unContrat1 = new Contrat(1, "Emmenagement local", "Truc SA");
		Contrat unContrat2 = new Contrat(2, "Emmenagement externe", "Machin SA");
		lesContrats.add(unContrat1);
		lesContrats.add(unContrat2);
		
		
		/*
		 * CODE GENERE - FENETRE PRINCIPALE, ONGLETS ET BOUTONS
		 * DEBUT
		 */
		setTitle("TempoFixe");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		btnAjouter = new JButton("Ajouter");
		btnAjouter.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (tabbedPane.getSelectedIndex() == 1) {
					monIhmTache.main();
					monIhmTache.setCboMatriculeCollab(lesCollabs);
					monIhmTache.setCboRefContrat(lesContrats);
				}
			}
		});
		btnModifier = new JButton("Modifier");
		btnModifier.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (tabbedPane.getSelectedIndex() == 1) {
					monIhmTache.main();

					monIhmTache.setCboMatriculeCollab(lesCollabs);
					monIhmTache.setCboRefContrat(lesContrats);
					monIhmTache.modifierTache(avoirTache());
				}
			}
		});
		btnSupprimer = new JButton("Supprimer");
		btnSupprimer.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
			}
		});
		btnQuitter = new JButton("Quitter");
		btnQuitter.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				System.exit(0);
			}
		});
		gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(btnAjouter)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnModifier)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnSupprimer)
					.addPreferredGap(ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
					.addComponent(btnQuitter))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, 219, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnAjouter)
						.addComponent(btnModifier)
						.addComponent(btnSupprimer)
						.addComponent(btnQuitter)))
		);
		/*
		 * CODE GENERE - FENETRE PRINCIPALE, ONGLETS ET BOUTONS
		 * FIN
		 */
		
		/*
		 * CODE GENERE - ONGLET COLLABORATEUR
		 * DEBUT
		 */
		JPanel pnlCollaborateur = new JPanel();
		tabbedPane.addTab("Collaborateur", null, pnlCollaborateur, null);
		JLabel lblCollabs = new JLabel("Liste des collaborateurs :");
		cboCollabs = new JComboBox<Collaborateur>();
		cboCollabs.addItem(unCollab1);
		cboCollabs.addItem(unCollab2);
		JLabel lblMatricule = new JLabel("Matricule :");
		JLabel lblNom = new JLabel("Nom :");
		JLabel lblPrenom = new JLabel("Prénom :");
		JLabel lblRepMatricule = new JLabel("NewLabel");
		JLabel lblRepNom = new JLabel("NewLabel");
		JLabel lblRepPrenom = new JLabel("NewLabel");
		GroupLayout gl_pnlCollaborateur = new GroupLayout(pnlCollaborateur);
		gl_pnlCollaborateur.setHorizontalGroup(
			gl_pnlCollaborateur.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pnlCollaborateur.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_pnlCollaborateur.createParallelGroup(Alignment.LEADING)
						.addComponent(lblCollabs, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
						.addComponent(cboCollabs, Alignment.TRAILING, 0, 399, Short.MAX_VALUE)
						.addGroup(gl_pnlCollaborateur.createSequentialGroup()
							.addGroup(gl_pnlCollaborateur.createParallelGroup(Alignment.TRAILING)
								.addComponent(lblMatricule, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(lblPrenom, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE)
								.addComponent(lblNom, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 50, Short.MAX_VALUE))
							.addGap(126)
							.addGroup(gl_pnlCollaborateur.createParallelGroup(Alignment.LEADING)
								.addComponent(lblRepPrenom, GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
								.addComponent(lblRepNom, GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
								.addComponent(lblRepMatricule, GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE))))
					.addContainerGap())
		);
		gl_pnlCollaborateur.setVerticalGroup(
			gl_pnlCollaborateur.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pnlCollaborateur.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblCollabs)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(cboCollabs, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGroup(gl_pnlCollaborateur.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_pnlCollaborateur.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblMatricule)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_pnlCollaborateur.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblNom)
								.addComponent(lblRepNom))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_pnlCollaborateur.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblPrenom)
								.addComponent(lblRepPrenom)))
						.addGroup(gl_pnlCollaborateur.createSequentialGroup()
							.addGap(6)
							.addComponent(lblRepMatricule)))
					.addContainerGap(80, Short.MAX_VALUE))
		);
		/*
		 * CODE GENERE - ONGLET COLLABORATEUR
		 * FIN
		 */
		
		
		
		/*
		 * CODE GENERE - ONGLET TACHE
		 * DEBUT
		 */
		pnlTache = new JPanel();
		lblNumero = new JLabel("Numéro :");
		lblLibelle = new JLabel("Libellé :");
		lblJourHomme = new JLabel("Jour/Homme :");
		lblMatriculeCollab = new JLabel("Matricule du collaborateur :");
		lblRefContrat = new JLabel("Référence du contrat :");
		lblRepNumero = new JLabel("New label");
		lblRepLibelle = new JLabel("New label");
		lblRepJourHomme = new JLabel("New label");
		lblRepMatriculeCollab = new JLabel("New label");
		lblRepRefContrat = new JLabel("New label");
		cboTaches = new JComboBox<Tache>();
		lblTaches = new JLabel("Liste des tâches :");
		
		int n = lesTaches.size();
		for (int i=0; i<n ; i++) {
			cboTaches.addItem(lesTaches.get(i));
		}
		this.consulterTache();
		
		pnlCollaborateur.setLayout(gl_pnlCollaborateur);
		tabbedPane.addTab("Tâche", null, pnlTache, null);
		
		cboTaches.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				consulterTache();
			}
		});
		
		
		/*
		 * Fin ajout
		 */
		gl_pnlTache = new GroupLayout(pnlTache);
		gl_pnlTache.setHorizontalGroup(
			gl_pnlTache.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pnlTache.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_pnlTache.createParallelGroup(Alignment.LEADING)
						.addComponent(lblTaches, GroupLayout.PREFERRED_SIZE, 399, GroupLayout.PREFERRED_SIZE)
						.addComponent(cboTaches, GroupLayout.PREFERRED_SIZE, 399, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_pnlTache.createSequentialGroup()
							.addGroup(gl_pnlTache.createParallelGroup(Alignment.LEADING)
								.addComponent(lblNumero, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblLibelle)
								.addComponent(lblJourHomme)
								.addComponent(lblMatriculeCollab)
								.addComponent(lblRefContrat))
							.addGap(71)
							.addGroup(gl_pnlTache.createParallelGroup(Alignment.LEADING)
								.addComponent(lblRepLibelle, GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
								.addComponent(lblRepJourHomme, GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
								.addComponent(lblRepMatriculeCollab, GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
								.addComponent(lblRepRefContrat, GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
								.addComponent(lblRepNumero, GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE))))
					.addContainerGap(62, Short.MAX_VALUE))
		);
		gl_pnlTache.setVerticalGroup(
			gl_pnlTache.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pnlTache.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblTaches)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(cboTaches, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGroup(gl_pnlTache.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_pnlTache.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblNumero)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblLibelle)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblJourHomme)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblMatriculeCollab)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblRefContrat))
						.addGroup(gl_pnlTache.createSequentialGroup()
							.addGap(6)
							.addComponent(lblRepNumero)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblRepLibelle)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblRepJourHomme)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblRepMatriculeCollab)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblRepRefContrat)))
					.addContainerGap(40, Short.MAX_VALUE))
		);
		pnlTache.setLayout(gl_pnlTache);
		/*
		 * CODE GENERE - ONGLET TACHE
		 * FIN
		 */
		
		
		/*
		 * CODE GENERE - ONGLET CONTRAT
		 * DEBUT
		 */
		JPanel pnlContrat = new JPanel();
		tabbedPane.addTab("Contrat", null, pnlContrat, null);
		
		JLabel lblContrats = new JLabel("Liste des contrats :");
		cboContrats = new JComboBox<Contrat>();
		cboContrats.addItem(unContrat1);
		cboContrats.addItem(unContrat2);
		JLabel lblReference = new JLabel("Référence :");
		JLabel lblObjetContrat = new JLabel("Objet du contrat :");
		JLabel lblNomClient = new JLabel("Nom du client :");
		JLabel lblRepReference = new JLabel("NewLabel");
		JLabel lblRepObjetContrat = new JLabel("NewLabel");
		JLabel lblRepNomClient = new JLabel("NewLabel");
		GroupLayout gl_pnlContrat = new GroupLayout(pnlContrat);
		gl_pnlContrat.setHorizontalGroup(
			gl_pnlContrat.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pnlContrat.createSequentialGroup()
					.addGroup(gl_pnlContrat.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_pnlContrat.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_pnlContrat.createParallelGroup(Alignment.LEADING)
								.addComponent(lblReference)
								.addComponent(lblNomClient)))
						.addGroup(gl_pnlContrat.createSequentialGroup()
							.addGap(10)
							.addComponent(lblObjetContrat)))
					.addGap(89)
					.addGroup(gl_pnlContrat.createParallelGroup(Alignment.LEADING)
						.addComponent(lblRepReference, GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
						.addComponent(lblRepNomClient, GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
						.addComponent(lblRepObjetContrat, GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE))
					.addContainerGap())
				.addGroup(gl_pnlContrat.createSequentialGroup()
					.addGap(10)
					.addComponent(lblContrats, GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
					.addGap(10))
				.addGroup(gl_pnlContrat.createSequentialGroup()
					.addGap(10)
					.addComponent(cboContrats, 0, 399, Short.MAX_VALUE)
					.addGap(10))
		);
		gl_pnlContrat.setVerticalGroup(
			gl_pnlContrat.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_pnlContrat.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblContrats)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(cboContrats, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_pnlContrat.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblReference)
						.addComponent(lblRepReference))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_pnlContrat.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblObjetContrat)
						.addComponent(lblRepObjetContrat))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_pnlContrat.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNomClient)
						.addComponent(lblRepNomClient))
					.addContainerGap(80, Short.MAX_VALUE))
		);
		pnlContrat.setLayout(gl_pnlContrat);
		contentPane.setLayout(gl_contentPane);
		/*
		 * CODE GENERE - ONGLET CONTRAT
		 * FIN
		 */
	}
	
	private void consulterTache(){
		int nbItems;
		Tache maTache;
		nbItems = cboTaches.getItemCount();
		if (nbItems != 0) {
			maTache = avoirTache();
			lblRepNumero.setText(Integer.valueOf(maTache.getNumTache()).toString());
			lblRepLibelle.setText(maTache.getLibelle());
			lblRepJourHomme.setText(Integer.valueOf(maTache.getChargePreviJoursHomme()).toString());
			lblRepMatriculeCollab.setText(Integer.valueOf(maTache.getChargePreviJoursHomme()).toString());
			lblRepRefContrat.setText(Integer.valueOf(maTache.getReferenceContrat()).toString());
		}
	}
	
	private Tache avoirTache(){
		int unId =((Tache)cboTaches.getSelectedItem()).getNumTache();
		Tache maTache = null;
		Tache tacheCourante;
		for	(int i = 0; i< lesTaches.size(); i++) {
			tacheCourante=lesTaches.get(i);
			if (tacheCourante.getNumTache()==unId) {
				maTache = tacheCourante;
			}
		}
		return maTache;
	}
	
	public ArrayList<Tache> getTaches() {
		return lesTaches;
	}
	
	public void ajoutTache(){
		
	}
	
	public void modificationTache(){
		
	}
	
	public void suppressionTache(){
		
	}
}

